package com.example.appmanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.MainActivity;
import com.example.appmanager.R;
import com.example.appmanager.entity.AppEntity;
import com.example.appmanager.ui.home.HomeFragment;
import com.example.appmanager.user.Login;
import com.example.appmanager.user.OneAppInfo;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppViewHolder> {
    private Context context;
    private String UName;

    public String getUName() {
        return UName;
    }

    public void setUName(String UName) {
        this.UName = UName;
    }

    private List<AppEntity.RowsBean> appEntities;

    public AppAdapter(Context context, List<AppEntity.RowsBean> appEntities) {
        this.context = context;
        this.appEntities = appEntities;

    }

    public AppAdapter() {

    }
    public void setData(Context context, List<AppEntity.RowsBean> appEntities) {
        this.context = context;
        this.appEntities = appEntities;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.app_all_show, parent, false);
        return new AppViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AppViewHolder holder, int position) {


        if (!(appEntities.isEmpty())) {//网络请求成功！

            Log.d("AppAdapter", String.valueOf(appEntities.size()));

            AppEntity.RowsBean appEntity = appEntities.get(position);
            Log.d("AppAdapter", appEntity.getAppname());
            holder.textName.setText(appEntity.getAppname());
            holder.textInfo.setText(appEntity.getAppinfo());
            if (appEntity.getPicture() == null) {
                Glide.with(context).load(Appconfig.BASE_IMAG + "APP.png").into(holder.imageView);
            } else {
                Glide.with(context).load(Appconfig.BASE_IMAG + appEntity.getPicture()).into(holder.imageView);
            }
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /*这里跳转到下载页面*/
                    Bundle bundle =  new Bundle();
                    bundle.putString("appId",String.valueOf(appEntity.getId()));
                    JSON app = (JSON) JSONObject.toJSON(appEntity);
                    Log.d("AppAdapter",app.toJSONString());
                    /*//通用传递数据
                    Bundle bundle = new Bundle();
                    //第一个参数为 标记 ，第二个参数为 值
                    bundle.putString("a","123");
                    //发送出去
                    one.setArguments(bundle);*/
                    bundle.putString("AppAdapter",String.valueOf(app));
                    Log .d("AppAdapter的APP信息：",String.valueOf(app));
                    bundle.putString("userLoginInfo",String.valueOf(100));//从用户主页跳转到我的下载里面
                    Intent intent = new Intent(v.getContext(), HomeFragment.class);//这里跳转到下载页面
                    intent.putExtras(bundle);
                    v.getContext().startActivity(intent);
                }
            });
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle =  new Bundle();
                    bundle.putString("appId",String.valueOf(appEntity.getId()));
                    JSON app = (JSON) JSONObject.toJSON(appEntity);
                    Log.d("AppAdapter",app.toJSONString());
                    bundle.putString("AppAdapter",String.valueOf(app));

                    Intent intent = new Intent(v.getContext(), OneAppInfo.class);//这里跳转到APP详情页面
                    intent.putExtras(bundle);
                    v.getContext().startActivity(intent);

                }
            });
        } else {
            Toast.makeText(context, "数据错误，请检查网络是否连接成功！", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public int getItemCount() {
        return appEntities == null ? 0 : appEntities.size();
    }

    public class AppViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;//APP图标
        private TextView textName;//APP名称
        private TextView textInfo;//APP简介
        private Button button;//这个是安装的按钮
        private LinearLayout linearLayout;//

        public AppViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.app_all_show_image);//APP图标
            textName = itemView.findViewById(R.id.app_all_name);//APP名称
            textInfo = itemView.findViewById(R.id.app_all_show_info);//APP简介
            button = itemView.findViewById(R.id.app_all_install);//这个是安装的按钮

            /*点击这个跳转到APP的详细信息里面去*/
            linearLayout = itemView.findViewById(R.id.linearLayout);//这个是linearLayout的监听

        }
    }
}
